import { _decorator } from 'cc';
import * as fgui from "fairygui-cc";
import AbstractUIWindow from '../../window/AbstractUIWindow';
import UICore from '../../../../Core/Res/UICore';
import ModuleWindow from '../../window/ModuleWindow';
import WindowObject from '../../window/WindowObject';
import SdkTool from '../../../../Core/Sdk/SdkTool';
import { Plane } from '../../view/game/component/Plane';
import { Grid } from '../../view/game/component/Grid';
import { ViewManager } from '../../base/ViewManager';
import { SocketManager } from '../../socket/SocketManager';
import { SkillItemRender } from '../../view/game/component/SkillItemRender';
import { ExternalGLoader } from '../../../../Core/Layer/ExternalGLoader';
import ProtoBufManager from '../../../proto/ProtoBufManager';
import EventMgr from '../../../../Core/Event/EventMgr';
import { GameModels } from '../../socket/GameModels';
import { DateUtils } from '../../../../Core/Utils/DateUtils';
import TimerMgr from '../../../../Core/Timer/TimerMgr';
import ConfigManager from '../../../Config/ConfigManager';
export class MainView extends AbstractUIWindow {
    private bg_full: fgui.GLoader;
    private share: fgui.GButton;
    private setting: fgui.GButton;
    private robot: fgui.GButton;
    private rank: fgui.GButton;
    private help: fgui.GButton;
    private formation: fgui.GButton;
    private randomBtn: fgui.GButton;
    private roomBtn: fgui.GButton;
    private warRecord: fgui.GButton;
    private sidebarBtn: fgui.GButton;
    private turntable: fgui.GButton;
    private logo: ExternalGLoader;
    private dailyAward: fgui.GButton;
    private countDownText: fgui.GTextField;
    private countDownNumber: number;
    private playerCount: fgui.GTextField;
    protected getResList(): Array<string> {
        return [`Main`, `Plane`];
    }
    protected onInit(): void {
        UICore.registerExtension(`Plane`, "Plane", Plane);
        UICore.registerExtension(`Plane`, "Grid", Grid);
        UICore.registerExtension(`Plane`, `SkillItemRender`, SkillItemRender);
        this._view = UICore.createObject("Main", "MainView").asCom;
        this.notClose = true;
    }
    protected onShow(): void {
        this.bg_full.url = `bg5.jpg`;
        EventMgr.on(`event_RespAwardInfo`, this.event_RespAwardInfo, this);
        EventMgr.on(`event_RespHeartBeat`, this.event_RespHeartBeat, this);
        this.countDownText = this.dailyAward.getChild(`countDown`) as fgui.GTextField;
        this.share.onClick(this.clickshare, this);
        this.setting.onClick(this.clickSetting, this);
        this.formation.onClick(this.clickFormation, this);
        this.robot.onClick(this.clickRobot, this);
        this.randomBtn.onClick(this.clickRandom, this);
        this.help.onClick(this.clickHelp, this);
        this.rank.onClick(this.clickRank, this);
        this.roomBtn.onClick(this.clickroomBtn, this);
        this.warRecord.onClick(this.clickWarRecord, this);
        this.turntable.onClick(this.clickturntable, this);
        if (SdkTool.ins.channelId == ProtoBufManager.ins.common.EChannelId.Visitor) {
            this.logo.onClick(this.clickLogo, this);
        }
        this.sidebarBtn.onClick(this.clicksidebarBtn, this);
        this.dailyAward.onClick(this.clickdailyAward, this);
        this.countDownText.text = ``;
        GameModels.support.send_ReqAwardInfo();
        this.playerCount.text = ``;
    }
    private event_RespHeartBeat(count: number): void {
        this.playerCount.text = `当前服务器在线人数:${count}`;
    }
    private event_RespAwardInfo(): void {
        if (GameModels.support.isSide === 1) {
            this.sidebarBtn.visible = false;
        } else {
            this.sidebarBtn.visible = SdkTool.ins.sidebar && GameModels.support.isSide === 0;
        }
        const sign: number = Math.round((Date.now() / 1000) - GameModels.support.dailyRewardSign);
        if (sign > DateUtils.ins.one_day) {
            this.countDownNumber = 0;
            this.countDownText.text = "";
        } else {
            this.countDownNumber = DateUtils.ins.one_day - sign;
            this.countDownText.text = DateUtils.ins.formatTime(this.countDownNumber);
            TimerMgr.ins.remove(this.countDown, this);
            TimerMgr.ins.add(1000, -1, this.countDown, this);
        }
    }
    private countDown(): void {
        if (this.countDownNumber <= 0) {
            this.countDownText.text = ``;
            TimerMgr.ins.remove(this.countDown, this);
            return;
        }
        this.countDownText.text = DateUtils.ins.formatTime(this.countDownNumber--);
    }
    private clickdailyAward(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        if (this.countDownNumber > 0) {
            ViewManager.ins.showtips("领奖时间未到!");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.DailyAwardWindow);
    }
    private clickLogo(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.TestWindow);
    }
    private clickturntable(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.TurntableWindow);
    }
    private clicksidebarBtn(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.SidebarWindow);
    }
    private clickWarRecord(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.GameLogWindow);

    }
    private clickroomBtn(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.RoomWindow);
    }
    private clickRandom(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openView(WindowObject.ins.PlayView);
    }
    protected clickRobot(): void {
        ModuleWindow.ins.openView(WindowObject.ins.RobotView);
    }
    private clickshare(): void {
        SdkTool.ins.sdk.shareAppMessage(`快来和我一起玩吧！`);
    }
    private clickSetting(): void {
        ModuleWindow.ins.openWindow(WindowObject.ins.SettingWindow);
    }
    private clickFormation(): void {
        ModuleWindow.ins.openWindow(WindowObject.ins.FormationWindow);
    }
    private clickHelp(): void {
        ModuleWindow.ins.openWindow(WindowObject.ins.TeachingWindow);
    }
    private clickRank(): void {
        if (SocketManager.ins.wsIsClose) {
            ViewManager.ins.showtips("未连接到服务器，请先连接！");
            return;
        }
        ModuleWindow.ins.openWindow(WindowObject.ins.RankWindow);
    }
}



